package com.lgy.service.impl;

import com.lgy.dao.CarbrandDao;
import com.lgy.pojo.CarBrand;
import com.lgy.pojo.Carseries;
import com.lgy.service.ICarBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarBrandServiceImpl implements ICarBrandService {
    @Autowired
    private CarbrandDao carbrandDao;

    @Override
    public List<CarBrand> findAll() {
        return carbrandDao.findAll();
    }

    @Override
    public List<Map<String, Object>> loadBrandTree() {
        // 汽车品牌树
        List<Map<String, Object>> tree = new ArrayList<>();

        //获取所有的品牌 以及 车系
        List<CarBrand> carBrandList =  carbrandDao.findCarAll();
        for (CarBrand carBrand : carBrandList) {
            Map<String,Object> treeNode = new HashMap<String, Object>();
            treeNode.put("text",carBrand.getBrandName());
            treeNode.put("BrandId",carBrand.getBrandId());
            //一级品牌的二级车系列信息
            List<Carseries> carSeriesList = carBrand.getCarSeriesList();
            if(carSeriesList!=null&&carSeriesList.size()>0){
                //存放二级车系集合
                List<Map<String,String>> seriesTree = new ArrayList<>();
                for (Carseries carseries : carSeriesList) {
                    HashMap<String, String> seriesMap = new HashMap<>();
                    seriesMap.put("text",carseries.getSeriesName());
                    seriesMap.put("seriesId",carseries.getSeriesId()+"");
                    seriesTree.add(seriesMap);
                    treeNode.put("nodes",seriesTree);
                }
            }
            tree.add(treeNode);
        }
        return tree;
    }
}
